package com.ruoyi.dl.wxPay;

import com.ruoyi.dl.wxPay.util.XMLUtil;
import org.jdom2.JDOMException;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeMap;

public class WXJSPay {

	/*public static void main(String[] args) throws Exception {
//		 String out_trade_num=System.currentTimeMillis()+"";
//		 String openid="oNWCiuCHiHRpKPec3NhlLodpTNa8";
//		 Map<String, String> wxPayParamMap  = jsApiPay(openid, "1",out_trade_num);//测试金额为1分钱

		nativePay("20200622172149491", "000", "1");

		//appJsApiPay("1", "2020515202215");
	}*/

	// Native支付请求二维码
	public static String nativePay(String out_trade_no, String product_id, String total_fee) throws Exception {
		String noceStr = Sha1Util.getNonceStr();// 随机字符串
		TreeMap<String, String> paramMap = new TreeMap<String, String>();
		paramMap.put("appid", Constants.APPID); // appid：每个公众号都有一个appid
		paramMap.put("body", Constants.BODY); // 商品描述
		paramMap.put("mch_id", Constants.MCH_ID); // 商户号：开通微信支付后分配
		paramMap.put("nonce_str", noceStr); // 随机字符串
		paramMap.put("notify_url", Constants.NOTIFY_URL); // 支付成功后，回调地址
		paramMap.put("out_trade_no", out_trade_no); // 商户订单号
		paramMap.put("product_id", product_id); // 商户根据自己业务传递的参数 当trade_type=NATIVE时必填
		paramMap.put("spbill_create_ip", Tool.getip()); // 本机的Ip
		paramMap.put("total_fee", total_fee); // 金额必须为整数 单位为分
		paramMap.put("trade_type", "NATIVE"); // 交易类型
		String wxpackage = WeiXinSignAndPackage.createPackage(paramMap);
		paramMap.put("sign", wxpackage);// 根据微信签名规则，生成签名。随机参数可以在商户后台管理系统中进行设置。
		String result=WeiXinSignAndPackage.getPrepayId(paramMap);//调用统一接口返回的值
		System.out.println("result = " + result);
		Map<String, String> map = XMLUtil.doXMLParse(result);// 调用统一接口返回的值转换为XML格式
		String code_url = map.get("code_url");
		System.out.println("code_url = " + code_url);
		return code_url;
	}

	// 内部支付(小程序、公众号)
	public static Map<String, String> jsApiPay(String openid, String total_fee, String out_trade_num)
			throws JDOMException, IOException {
		String noceStr = Sha1Util.getNonceStr();// 随机字符串
		String timeStamp = Sha1Util.getTimeStamp();// 时间戳
		// 接口package部分-内容----------------------------------------------------------
		TreeMap<String, String> contentMap = new TreeMap<String, String>();
		contentMap.put("appid", Constants.APPID); // 公众账号 ID
		contentMap.put("mch_id", Constants.MCH_ID); // 商户号
		contentMap.put("nonce_str", noceStr); // 随机字符串
		contentMap.put("body", Constants.BODY); // 商品描述
		contentMap.put("out_trade_no", out_trade_num); // 商户订单号
		contentMap.put("total_fee", total_fee); // 订单总金额
		contentMap.put("spbill_create_ip", Tool.getip()); // 订单生成的机器IP
		contentMap.put("notify_url", Constants.NOTIFY_URL); // 通知地址
		contentMap.put("trade_type", Constants.TRADE_TYPE_JS); // 交易类型
		contentMap.put("openid", openid); // 用户标识
		String wxpackage = WeiXinSignAndPackage.createPackage(contentMap);
		// System.out.println("wxpackage:::::::::::::::"+wxpackage);
		contentMap.put("sign", wxpackage);
		String result = WeiXinSignAndPackage.getPrepayId(contentMap);// 调用统一接口返回的值
		// System.out.println("result:::::::::::"+result);
		//// System.out.println("result="+result);
		Map<String, String> map = XMLUtil.doXMLParse(result);// 调用统一接口返回的值转换为XML格式
		// System.out.println(map);
		String return_code = (String) map.get("return_code");// 返回状态码
		// System.out.println(map);
		TreeMap<String, String> wxPayParamMap = new TreeMap<String, String>();
		if (return_code.equals("SUCCESS")) {
			wxPayParamMap.put("appId", Constants.APPID);
			wxPayParamMap.put("timeStamp", timeStamp);
			wxPayParamMap.put("nonceStr", noceStr);
			wxPayParamMap.put("package", "prepay_id=" + map.get("prepay_id"));
			wxPayParamMap.put("signType", Constants.SIGNTYPE);
			String paySign = WeiXinSignAndPackage.createPaySign(wxPayParamMap);// 支付得到的签名
			// System.out.println("paySign:::::::::::"+paySign);
			wxPayParamMap.put("paySign", paySign);
			wxPayParamMap.put("payMoney", total_fee);// 到前段显示使用，支付不需要此参数
		}
		return wxPayParamMap;
	}

	// 外部支付(APP)
	/*public static Map<String, String> appJsApiPay(String total_fee, String out_trade_num)
			throws JDOMException, IOException {
		String noceStr = Sha1Util.getNonceStr();// 随机字符串
		String timeStamp = Sha1Util.getTimeStamp();// 时间戳
		// 接口package部分-内容----------------------------------------------------------
		TreeMap<String, String> contentMap = new TreeMap<String, String>();
		contentMap.put("appid", Constants.APPID); // 公众账号 ID
		contentMap.put("body", Constants.BODY); // 商品描述
		contentMap.put("mch_id", Constants.MCH_ID); // 商户号
		contentMap.put("nonce_str", noceStr); // 随机字符串
		contentMap.put("notify_url", Constants.NOTIFY_URL); // 通知地址
		contentMap.put("out_trade_no", out_trade_num); // 商户订单号
		contentMap.put("spbill_create_ip", Tool.getip()); // 订单生成的机器IP
		contentMap.put("total_fee", total_fee); // 订单总金额
		contentMap.put("trade_type", Constants.TRADE_TYPE_JS); // 交易类型
		String wxpackage = WeiXinSignAndPackage.appCreatePackage(contentMap);
		System.out.println("wxpackage:::::::::::::::" + wxpackage);//
		contentMap.put("sign", wxpackage);
		String result = WeiXinSignAndPackage.appGetPrepayId(contentMap);// 调用统一接口返回的值
		System.out.println("result:::::::::::" + result);
		System.out.println("result=" + result);
		Map<String, String> map = XMLUtil.doXMLParse(result);// 调用统一接口返回的值转换为XML格式
		System.out.println(":::::::::::::::::::::::::::::::::::::::" + result);
		System.out.println(map);//
		TreeMap<String, String> wxPayParamMap = new TreeMap<String, String>();
		wxPayParamMap.put("appid", Constants.APPID);
		wxPayParamMap.put("partnerid", Constants.MCH_ID);
		wxPayParamMap.put("timestamp", timeStamp);
		wxPayParamMap.put("noncestr", noceStr);
		wxPayParamMap.put("prepayid", map.get("prepay_id"));
		wxPayParamMap.put("package", "Sign=WXPay");
		String paySign = WeiXinSignAndPackage.appCreatePaySign(wxPayParamMap);// 支付得到的签名
		System.out.println("paySign:::::::::::" + paySign);
		System.out.println("prepay_id--------" + map.get("prepay_id"));
		wxPayParamMap.put("sign", paySign);
		//wxPayParamMap.put("payMoney", total_fee);// 到前段显示使用，支付不需要此参数
		return wxPayParamMap;
	}*/

	/**
	 * 企业向个人账户付款
	 * 
	 * @param transferId
	 * @param openId
	 * @param transferMoney
	 * @param ip
	 * @return
	 */
	public static Map<String, String> transferPay(String transferId, String openId, BigDecimal transferMoney) {
		Map<String, String> restmap = null;
		String noceStr = Sha1Util.getNonceStr();// 随机字符串
		TreeMap<String, String> parm = new TreeMap<String, String>();
		parm.put("mch_appid", Constants.APPID); // 公众账号appid
		parm.put("mchid", Constants.MCH_ID); // 商户号
		parm.put("nonce_str", noceStr); // 随机字符串
		parm.put("partner_trade_no", transferId); // 商户订单号
		parm.put("openid", openId); // 用户openid
		parm.put("check_name", "NO_CHECK"); // 校验用户姓名选项 OPTION_CHECK
		// parm.put("re_user_name", "安迪"); //check_name设置为FORCE_CHECK或OPTION_CHECK，则必填
		parm.put("amount", transferMoney.toString()); // 转账金额
		parm.put("desc", "提现"); // 企业付款描述信息
		//parm.put("spbill_create_ip", ip); // Ip地址
		parm.put("sign", WeiXinSignAndPackage.createPaySign(parm));
		String restxml = HttpTool.sendPost(Constants.TRANSFERS_PAY, parm, "utf-8");
		try {
			restmap = XMLUtil.doXMLParse(restxml);
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return restmap;
	}

	/**
	 * 企业转账到用户的银行卡
	 * 
	 * @return
	 */
	/*public static Map<String, String> transferPay_bank(String transferId, String bank_no, String bank_code,
			String true_name, BigDecimal transferMoney) {
		String publicKeyPKCS8 = Constants.publicKeyPKCS8;
		String noceStr = Sha1Util.getNonceStr();// 随机字符串
		Map<String, String> result_map = null;
		try {
			String enc_true_name = GetRSA.getRSA(true_name, publicKeyPKCS8);
			String enc_bank_no = GetRSA.getRSA(bank_no, publicKeyPKCS8);
			TreeMap<String, String> parm = new TreeMap<String, String>();
			parm.put("mch_id", Constants.MCH_ID);
			parm.put("partner_trade_no", transferId);
			parm.put("nonce_str", noceStr);
			parm.put("enc_bank_no", enc_bank_no);
			parm.put("enc_true_name", enc_true_name);
			parm.put("bank_code", bank_code);
			parm.put("amount", Tool.getMoney(transferMoney.toString()));
			parm.put("desc", "企业付款到个人账户");
			String sign = WeiXinSignAndPackage.createPaySign(parm);
			TreeMap<String, String> parm2 = new TreeMap<String, String>();
			parm2.put("mch_id", Constants.MCH_ID);
			parm2.put("partner_trade_no", transferId);
			parm2.put("nonce_str", noceStr);
			parm2.put("enc_bank_no", enc_bank_no);
			parm2.put("enc_true_name", enc_true_name);
			parm2.put("bank_code", bank_code);
			parm2.put("amount", Tool.getMoney(transferMoney.toString()));
			parm2.put("desc", "企业付款到个人账户");
			parm2.put("sign", sign);
			String pay_xml = XMLUtils.getRequestXml(parm2);
			String result_str = HttpClientCustomSSL.httpClientResultPANK(pay_xml);
			result_map = XMLUtil.doXMLParse(result_str);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return result_map;
	}
*/
	// 返回前端--微信公众号支付所需要的参数
	public static Map<String, String> wxPayParameter(String openId, String total_fee, String orderId) {
		Map<String, String> wxPayParamMap = null;
		try {
			wxPayParamMap = WXJSPay.jsApiPay(openId, Tool.getMoney(total_fee), orderId);// 测试金额为1分钱
		} catch (JDOMException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("appId", wxPayParamMap.get("appId"));
		map.put("timeStamp", wxPayParamMap.get("timeStamp"));
		map.put("nonceStr", wxPayParamMap.get("nonceStr"));
		map.put("wxPackage", wxPayParamMap.get("package"));
		map.put("signType", wxPayParamMap.get("signType"));
		map.put("paySign", wxPayParamMap.get("paySign"));
		return map;
	}
}
